package cn.com.nes.site.service.crm.customer;

import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.site.entity.em.crm.customer.CustomerNatureEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CustomerBuilderStrategyFactory {

    @Autowired
    private List<CustomerBuilder> customerBuilders;


    private static Map<CustomerNatureEnum, CustomerBuilder> customerBuilderMap;

    @PostConstruct
    public void init() {
        customerBuilderMap = customerBuilders.stream().collect(Collectors.toMap(CustomerBuilder::getType, t -> t));
    }

    public CustomerBuilder getCustomerBuilder(CustomerNatureEnum customerNatureEnum) {
        CustomerBuilder customerBuilder = customerBuilderMap.get(customerNatureEnum);
        if (customerBuilder == null) {
            throw new BusinessException("未匹配的客户构建方式！");
        }
        return customerBuilder;
    }


}
